package com.spa.interfaces.admin;

import com.spa.application.command.admin.CityAddCommand;
import com.spa.application.command.admin.CityConfigTravelCommand;
import com.spa.application.command.admin.CityUpdateCommand;
import com.spa.application.dto.admin.CityDTO;
import com.spa.application.dto.admin.CityExportDTO;
import com.spa.application.query.admin.CityQuery;
import com.spa.application.service.admin.CityApplicationService;
import com.spa.infrastructure.common.ResponseMessage;
import com.spa.interfaces.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("admin/city")
public class AdminCityController extends BaseController {

    @Autowired
    private CityApplicationService cityApplicationService;

    @PostMapping("add")
    public ResponseMessage<Void> add(@Validated @RequestBody CityAddCommand command) {
        cityApplicationService.addCity(command);
        return responseHelper.success();
    }

    @PostMapping("all")
    public ResponseMessage<List<CityDTO>> list() {
        return responseHelper.success(cityApplicationService.findAllCities(null));
    }

    @PostMapping("export")
    public void export(HttpServletResponse response) throws Exception {
        List<CityExportDTO> list = cityApplicationService.findAllCitiesExport(null);
        exportQueryResult(response, list, "城市列表", "城市列表", CityExportDTO.class);
    }

    @PostMapping("getById")
    public ResponseMessage<CityDTO> getById(@Validated @RequestBody CityQuery query) {
        return responseHelper.success(cityApplicationService.getById(query.getId()));
    }

    @PostMapping("close")
    public ResponseMessage<Void> close(@Validated @RequestBody CityUpdateCommand command) {
        cityApplicationService.close(command);
        return responseHelper.success();
    }

    @PostMapping("open")
    public ResponseMessage<Void> open(@Validated @RequestBody CityUpdateCommand command) {
        cityApplicationService.open(command);
        return responseHelper.success();
    }

    @PostMapping("configTravel")
    public ResponseMessage<Void> configTravel(@Validated @RequestBody CityConfigTravelCommand command) {
        cityApplicationService.configTravel(command);
        return responseHelper.success();
    }

}
